<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="company" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-tree
          default-expand-all
          :data="data"
          :props="defaultProps"
        >
          <template slot-scope="{ node, data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <svg-icon icon-class="company" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
    >
      <!-- @close="hDialogClose" -->
      <!-- ref="refdeptDialog" -->
      <!-- 显示详情不准确 -->
      <!-- 解决方案1：在关闭弹层时，把子组件销毁 -->
      <dept-dialog
        v-if="showDialog"
        :pid="curId"
        :isedit="isEdit"
        :originlist="originList"
        @success="hSuccess"
        @close="hClose"
      />
    </el-dialog>
  </div>
</template>
<script>
import { tranListToTreeData } from '@/utils/index'
import { getDepartments, delDepartment } from '@/api/departments'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      isEdit: false,
      curId: '', // 当前选中部门id
      showDialog: false, // 显示或隐藏弹框
      data: [
        // {
        //   label: '总裁办'
        // },
        // {
        //   label: '财务部',
        //   children: [{
        //     label: '核算部'
        //   }]
        // }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      originList: []// 给子组件做检验的参考数据
    }
  },
  created() {
    // 发请求
    this.loadDepartments()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    // 获取部门列表
    async loadDepartments() {
      const res = await getDepartments()
      console.log(res)
      // 删除第一行
      res.data.depts.shift()
      this.data = tranListToTreeData(res.data.depts)
      // 加工数据originList，准备给子组件做校验使用
      this.originList = res.data.depts.map(item => {
        return { id: item.id, pid: item.pid, name: item.name, code: item.code }
      })
    },
    // 用户点击添加
    hAdd(id) {
      // 显示弹框
      this.showDialog = true
      // 获取当前id，传给子组件
      this.curId = id

      this.isEdit = false
    },
    hSuccess() {
      // 点击确认关闭弹框
      this.showDialog = false
      // 重新获取数据
      this.loadDepartments()
    },
    hClose() {
      this.showDialog = false
    },
    // 用户点击编辑
    hEdit(id) {
      // 更新当前id
      this.curId = id
      // 设置状态为编辑
      this.isEdit = true
      // 打开弹框
      this.showDialog = true

      // 显示详情不准确
      // 解决方案2：在父组件中通过引用找到子组件
      // 在父组件中，每次打开弹层时，找到子组件，要求它去发请求获取详情
      // this.$nextTick(() => {
      //   console.log('获取子组件的引用', this.$refs.refdeptDialog)
      //   // 调用子组件的方法
      //   this.$refs.refdeptDialog.loadDepartDetail()
      // })
    },
    // 用户点击删除
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    },
    async doDel(id) {
      // 调用接口
      await delDepartment(id)
      // 提示一下
      this.$message.success('删除成功')
      // 重新获取部门列表
      this.loadDepartments()
    }
    // hDialogClose() {
    //   // 找到子组件，调用重置表单方法
    //   this.$refs.refdeptDialog.resetForm()
    // }
  }
}
</script>
